package com.h5ve.wxmp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.h5ve.base.api.R;
import com.h5ve.base.constant.AppConstants;
import com.h5ve.wxmp.entity.WxUser;
import com.h5ve.wxmp.entity.WxUserTagsDict;
import com.h5ve.wxmp.service.WxUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserTagService;
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 微信用户标签
 *
 * @author www.joolun.com
 * @date 2019-03-25 15:39:39
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(AppConstants.APP_WXMP + "/wxTags")
public class WxTagsController {

    private final WxMpService wxService;
    private final WxUserService wxUserService;

    /**
     * 获取微信用户标签
     *
     * @return
     */
    @SaCheckPermission("wxTags_view")
    @GetMapping("/list")
    public R getWxUserList(String appId) {
        WxMpUserTagService wxMpUserTagService = wxService.getUserTagService();
        try {
            List<WxUserTag> listWxUserTag = wxMpUserTagService.tagGet();
            return R.ok(listWxUserTag);
        } catch (WxErrorException e) {
            e.printStackTrace();
            log.error("获取微信用户标签失败", e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 获取微信用户标签字典
     *
     * @param appId
     * @return
     */
    @SaCheckPermission("wxTags_view")
    @GetMapping("/dict")
    public R getWxUserTagsDict(String appId) {
        WxMpUserTagService wxMpUserTagService = wxService.getUserTagService();
        try {
            List<WxUserTag> listWxUserTag = wxMpUserTagService.tagGet();
            List<WxUserTagsDict> listWxUserTagsDict = new ArrayList<>();
            WxUserTagsDict wxUserTagsDict;
            for (WxUserTag wxUserTag : listWxUserTag) {
                wxUserTagsDict = new WxUserTagsDict();
                wxUserTagsDict.setName(wxUserTag.getName());
                wxUserTagsDict.setValue(wxUserTag.getId());
                listWxUserTagsDict.add(wxUserTagsDict);
            }
            return R.ok(listWxUserTagsDict);
        } catch (WxErrorException e) {
            e.printStackTrace();
            log.error("获取微信用户标签字典失败", e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 新增微信用户标签
     *
     * @return
     */
    @SaCheckPermission("wxTags_add")
    @PostMapping
    public R save(@RequestBody JSONObject data) {
        String appId = data.getStr("appId");
        String name = data.getStr("name");
        WxMpUserTagService wxMpUserTagService = wxService.getUserTagService();
        try {
            return R.ok(wxMpUserTagService.tagCreate(name));
        } catch (WxErrorException e) {
            e.printStackTrace();
            log.error("新增微信用户标签失败", e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改微信用户标签
     *
     * @return
     */
    @SaCheckPermission("wxTags_edit")
    @PutMapping
    public R updateById(@RequestBody JSONObject data) {
        String appId = data.getStr("appId");
        Long id = data.getLong("id");
        String name = data.getStr("name");
        WxMpUserTagService wxMpUserTagService = wxService.getUserTagService();
        try {
            return R.ok(wxMpUserTagService.tagUpdate(id, name));
        } catch (WxErrorException e) {
            e.printStackTrace();
            log.error("修改微信用户标签失败", e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除微信用户标签
     *
     * @param id
     * @param appId
     * @return
     */
    @SaCheckPermission("wxTags_del")
    @DeleteMapping
    public R removeById(Long id, String appId) {
        long count = wxUserService.count(Wrappers.<WxUser>lambdaQuery()
                .and(wrapper -> wrapper
                        .eq(WxUser::getTagidList, "[" + id + "]")
                        .or()
                        .like(WxUser::getTagidList, "," + id + ",")
                        .or()
                        .likeRight(WxUser::getTagidList, "[" + id + ",")
                        .or()
                        .likeLeft(WxUser::getTagidList, "," + id + "]")));
        if (count > 0) {
            return R.error("该标签下有用户存在，无法删除");
        }
        WxMpUserTagService wxMpUserTagService = wxService.getUserTagService();
        try {
            return R.ok(wxMpUserTagService.tagDelete(id));
        } catch (WxErrorException e) {
            e.printStackTrace();
            log.error("删除微信用户标签失败", e);
            return R.error(e.getMessage());
        }
    }
}
